package server;
import server.Data;
import java.sql.ResultSet;
import java.sql.SQLException;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

import server.Person;

public class Staff extends UnicastRemoteObject implements StaffInterface
{
	private Data data;
	/*
	private int ID;
	private String post;
	private int level;
	private double salary;
	private int consumHp;
	private int consumHap;
	private int exp;
	private int upExp;
	
	public Staff(String post,int level,double salary,int consumHp,int consumHap,int exp,int upExp)
	{
		this.post=post;
		this.level=level;
		this.salary=salary;
		this.consumHp=consumHp;
		this.consumHap=consumHap;
		this.exp=exp;
		this.upExp=upExp;
	}
	*/
	public Staff() throws RemoteException
	{
		data=new Data();
	}
	

	public boolean isPromoted(int pexp,int upSexp) throws RemoteException
	{
		if(pexp>=upSexp)
			return true;
		else 
			return false;
	
	}
	
	
	public String getPost(int id)throws RemoteException
	{
		String sql;
		ResultSet rs=null;
		String post=null;
			
		sql="select * from comstaff where id=" + id;
		rs=data.getResultSet(sql);
		try {
			while(rs.next()){
				post=rs.getString("post");
		}
			} catch (SQLException e) {
			
			e.printStackTrace();
			}			
		return post;				
	}
	public String getPost(int comId,int lev)throws RemoteException
	{
		String sql;
		ResultSet rs=null;
		String post=null;
		sql="select * from comstaff where companyId= "+comId+ " and level=" +lev;

		rs=data.getResultSet(sql);
		try {
			while(rs.next()){
				post=rs.getString("post");
		}
			} catch (SQLException e) {
			
			e.printStackTrace();
			}			
		return post;				
	}
	
	public int getLevel(int comId,String post)throws RemoteException
	{
		
		String sql;
		ResultSet rs=null;
		int level=0;
			
		sql="select * from comstaff where companyId= "+comId+ " and post='" +post+"'";
		rs=data.getResultSet(sql);
		
		try {
			while(rs.next()){
				level=rs.getInt("level");
		}
			} catch (SQLException e) {
			
			e.printStackTrace();
			}		
		return level;
	}
	
	public double getSalary(int companyId,String post)throws RemoteException
	{
		String sql;
		ResultSet rs=null;
		double salary=0;	
		sql="select * from comstaff where post= '" +post+ "' and companyId="+companyId + " limit 1";
		rs=data.getResultSet(sql);
		try {
			while(rs.next()){
				salary=rs.getDouble("salary");
		}
			} catch (SQLException e) {
			
			e.printStackTrace();
			}		
		return salary;
	}
	
	public int getConsumHp(int companyId,String post)throws RemoteException
	{
		String sql;
		ResultSet rs=null;
		int consumHp=0;
			
		sql="select * from comstaff where post= '" +post+ "' and companyId="+companyId;
		rs=data.getResultSet(sql);
		try {
			while(rs.next()){
				consumHp=rs.getInt("consumhp");
		}
			} catch (SQLException e) {
			
			e.printStackTrace();
			}		
		return consumHp;
	}
	
	public int getConsumHap(int companyId,String post)throws RemoteException
	{
		String sql;
		ResultSet rs=null;
		int consumHap=0;
			
		sql="select * from comstaff where post= '" +post+ "' and companyId="+companyId;
		rs=data.getResultSet(sql);
		try {
			while(rs.next()){
				consumHap=rs.getInt("consumhap");
		}
			} catch (SQLException e) {
			
			e.printStackTrace();
			}		
		return consumHap;
	}
	
	public int getExp(int companyId,String post)throws RemoteException
	{
		String sql;
		ResultSet rs=null;
		int exp=0;
			
		sql="select * from comstaff where post= '" +post+ "' and companyId="+companyId;
		rs=data.getResultSet(sql);
		try {
			while(rs.next()){
				exp=rs.getInt("exp");
		}
			} catch (SQLException e) {
			
			e.printStackTrace();
			}		
		return exp;
	}
	public int getUpExp(int companyId,String post)throws RemoteException
	{
		String sql;
		ResultSet rs=null;
		int upExp=0;
			
		sql="select * from comstaff where post= '" +post+ "' and companyId="+companyId;
		rs=data.getResultSet(sql);
		try {
			while(rs.next()){
				upExp=rs.getInt("upExp");
		}
			} catch (SQLException e) {
			
			e.printStackTrace();
			}		
		return upExp;
	}
	
	public int getSize() throws RemoteException{
		String sql;
		ResultSet rs=null;
		int sum=0;
			
		sql="select count(*) as sum from comstaff";
		rs=data.getResultSet(sql);
		try {
			if(rs.next()){
				sum=rs.getInt("sum");
		}
			} catch (SQLException e) {
			
			e.printStackTrace();
			}		
		return sum;
	}
}
